﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autocentro.Entities;
using Autocentro.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Autocentro.UI
{
    public partial class FrmProveedor : Form
    {
        [Dependency]
        public IProveedorBL ProveedorBL { get; set; }
        public FrmProveedor()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void txtCriterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (rbRuc.Checked)
            {
                
                txtCriterio.MaxLength = 11;
                if (Char.IsDigit(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
            if (rbNom.Checked)
            {                 
                txtCriterio.MaxLength = 50;

                if (Char.IsLetter(e.KeyChar) || Char.IsSymbol(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            var frmProveedor = new FrmMttProveedor();
            frmProveedor.AddProveedor();
            frmProveedor.ShowDialog();
            pROVEEDORBindingSource.DataSource = ProveedorBL.GetAllFromProveedores();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var updateProveedor = pROVEEDORBindingSource.Current as PROVEEDOR;
            if (updateProveedor != null)
            {
                var frmProveedor = new FrmMttProveedor();
                frmProveedor.Currentproveedor = updateProveedor;
                frmProveedor.UpdateProveedor();
                frmProveedor.ShowDialog();
                pROVEEDORBindingSource.DataSource = ProveedorBL.GetAllFromProveedores();
            }
            else
            {
                MessageBox.Show("Debe de seleccionar un Proveedor");
            }
            
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            var removeProveedor = pROVEEDORBindingSource.Current as PROVEEDOR;
            if (removeProveedor!=null)
            {
                var rpt = MessageBox.Show(this,
                    string.Format("¿Desea eliminar el proveedor: {0}?", removeProveedor.RazonSocial),
                    "Mensaje",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                if (rpt == DialogResult.Yes)
                {
                    ProveedorBL.RemoveProveedor(removeProveedor.Id);
                    CargaData();
                }
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Proveedor");
            }
        }

        private void FrmProveedor_Load(object sender, EventArgs e)
        {
            CargaData();
        }

        private void CargaData()
        {
            var criterio = txtCriterio.Text == string.Empty ? null : txtCriterio.Text;
            pROVEEDORBindingSource.DataSource = ProveedorBL.GetFromProveedorByCriterio(criterio);
        }

        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CargaData();
            }
        }

        private void rbRuc_CheckedChanged(object sender, EventArgs e)
        {
            if (rbRuc.Checked)
            {
                txtCriterio.Text = "";
            }
            else
            {
                txtCriterio.Text="";
            }
        }

        public PROVEEDOR varProveedor { get; set; }
        private void pROVEEDORDataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            varProveedor = pROVEEDORBindingSource.Current as PROVEEDOR;
            this.Close();
        }

        public PROVEEDOR CurrentProveedor { get; set; }

        public bool Prove { get; set; }

        private void pROVEEDORDataGridView_DoubleClick(object sender, EventArgs e)
        {
            if (Prove == true)
            {
                CurrentProveedor = pROVEEDORBindingSource.Current as PROVEEDOR;
                Close();
            }
        }


    }
}
